package ui.tp3.TP3Wicket;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.util.file.File;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ImageButton;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.image.Image;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.ComponentPropertyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import ui.tp3.modelo.Coordenada;
import ui.tp3.modelo.Esfera;
import ui.tp3.modelo.Personaje;
import ui.tp3.modelo.Tablero;
import ui.tp3.modelo.UserException;

public class CreacionPersonajesPage extends WebPage {
	private static final long serialVersionUID = 2L;
	final private Tablero tablero;
	final private Personaje personaje;
	private String selected = "Goku";
	private Image img;
	private FeedbackPanel feedbackPanel;
	private FileUploadField fileUp;
	private List<FileUpload> imagenes;
	
	public List<FileUpload> getImagenes() {
		return imagenes;
	}

	public void setImagenes(List<FileUpload> imagenes) {
		this.imagenes = imagenes;
	}

	public FileUploadField getFileUp(){
		return this.fileUp;
	}
	
	public void setFileUp(FileUploadField fu){
		this.fileUp = fu;
	}

	public CreacionPersonajesPage(AccionesSobreElMapaPage accionesSobreElMapaPage) {
		
		feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
		feedbackPanel.setOutputMarkupId(true);
		
		this.fileUp = new FileUploadField("imgElegida");
		fileUp.setOutputMarkupId(true);
		fileUp.setVisible(false);
		fileUp.setModel(new PropertyModel<List<FileUpload>>(this, "imagenes"));

		final AccionesSobreElMapaPage acp = accionesSobreElMapaPage;
		this.tablero = accionesSobreElMapaPage.getTablero();

		this.personaje = new Personaje();

		Form<Personaje> form = new Form<Personaje>("formulario1",
				new CompoundPropertyModel<Personaje>(this.personaje));
		form.setOutputMarkupId(true);

		final Coordenada coord = new Coordenada();
		coord.setX(0);
		coord.setY(0);

		final ArrayList<String> personajesDisponibles = tablero
				.personajesDisponibles();

		final TextField<Integer> numeroColumna = new TextField<Integer>("x",
				new PropertyModel<Integer>(this.personaje.coordenada, "x"));
		final TextField<Integer> numeroFila = new TextField<Integer>("y",
				new PropertyModel<Integer>(this.personaje.coordenada, "y"));

		form.add(numeroColumna);
		form.add(numeroFila);

		final TextField<Integer> numeroMaxMov = new TextField<Integer>(
				"moveMax");
		form.add(numeroMaxMov);

		final RadioChoice<String> nombreDePersonajesDisponibles = new RadioChoice<String>(
				"nombre", new PropertyModel<String>(this, "selected"),
				personajesDisponibles);

		this.img = new Image("img", new ContextRelativeResource("/" + selected
				+ ".jpg"));
		img.setOutputMarkupId(true);

		form.add(this.img);

		nombreDePersonajesDisponibles
				.add(new AjaxFormChoiceComponentUpdatingBehavior() {
					@Override
					protected void onUpdate(AjaxRequestTarget target) {
						CreacionPersonajesPage.this.img
								.setImageResource(new ContextRelativeResource(
										"/"
												+ CreacionPersonajesPage.this.selected
												+ ".jpg"));
						target.add(CreacionPersonajesPage.this.img);

					}
				});

		form.add(nombreDePersonajesDisponibles);

		form.add(new Button("volver") {
			private static final long serialVersionUID = 1L;

			public void onSubmit() {
				this.setResponsePage(acp);
			}

		});

		form.add(new FeedbackAjaxButton("crearPersonaje", this.feedbackPanel) {
			@Override
			protected void doOnSubmit(AjaxRequestTarget target, Form<?> form) {
				personaje.setNombre(selected);
				personaje.setMapa(tablero);
				tablero.agregarPersonaje(personaje);

				this.setResponsePage(new AccionesSobreElMapaPage(tablero));
			}
		});
		
		//fileUp.setDefaultModelObject(this.getDefaultModelObject());
		
		form.add(new Button("cargarImg"){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
				
				//fileUp.setDefaultModelObject(CreacionPersonajesPage.this);
                FileUpload upload = fileUp.getFileUpload();
                if (upload == null){
                	nombreDePersonajesDisponibles.setVisible(false);
    				fileUp.setVisible(true);
                }else{
                	
                	
                	File newFile = new File("media/SASASA/TP3Wicket/src/main/webapp/"
        					+ upload.getClientFileName());
                	
                	if (newFile.exists()) {
    					newFile.delete();
    				}

						System.out.println("PATH "+newFile.getAbsolutePath());
	                	CreacionPersonajesPage.this.img.setImageResource(new ContextRelativeResource(newFile.getAbsolutePath()));
                	}
				}
			
		});

		form.add(fileUp);
		this.add(form);
		//this.add(this.fileUp);

	}

	public String getSelected() {
		return this.selected;
	}

	public void setSelected(String s) {
		this.selected = s;
	}

}
